<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class ActingMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        //这是一个接口加密中间件，接口加密参数(time,nonce,sign) 加密方式
        $time = $request->input('time');
        $type = $request->input('type');
        $sign = $request->input('sign');
        if(empty($time) || empty($type) || empty($sign)){
            return response()->json(['code'=>400,'msg'=>'参数错误']);
        }
        if(time() - $time > config("base.exp_time")){
            return response()->json(['code'=>400,'msg'=>'请求超时']);
        }
        $str = $time.$type.config("base.apikay");
        $sign_str = md5($str);
        if($sign_str != $sign){
            return response()->json(['code'=>400,'msg'=>'签名错误']);
        }
        return $next($request);
    }
}
